﻿using System;
using System.Web.UI.HtmlControls;

using OctoOS;
using OctoOS.UI;

public partial class _default : OOSPage
{
	int _itemsOnPage = 20;

	protected void Page_Load(object sender, EventArgs e)
	{
		var resourceInfo = AppUtil.GetResourceInfo(OOSRequest.Current);
		var settings = resourceInfo.Settings;
		var categories = resourceInfo.Categories;

		if(OOSRequest.Current.IsModerator)
			Commands.AddCommand(Resources.Main.default_AdminPage, Urls.Admin.GetDefault());

		Commands.AddCommand(Resources.Main.default_AddNew, Urls.GetAddArticle());
		Commands.AddCommand(Resources.Main.shared_MyArticlesLink, Urls.GetMyArticles());
		Commands.AddCommand(Resources.Main.default_AllArticles, Urls.GetAllArticles());

		if(settings.FeedMode != FeedMode.None)
		{
			Commands.AddCommand(new Command
			{
				Icon = "/images/rss.gif",
				HRef = Urls.Ajax.GetMainRss(false)
			});
		}

		if(categories.Length == 0)
		{
			emptyMsg.Visible = true;
			emptyMsg.SetContent(Resources.Main.default_EmptyMessage);
		}
		else
		{
			foreach(var category in categories)
			{
				categoryTree.Nodes.AddContentNode(
					new HtmlAnchor
					{
						ID = category.CategoryID.ToString(),
						InnerText = category.Title,
						HRef = Urls.GetCategory(category.CategoryID, category.Title)
					});
			}
		}

		int totalCount;
		var previews = ApiService.GetMainPreviews(OOSRequest.Current, 0,
			_itemsOnPage, out totalCount);

		if(previews.Length == 0)
		{
			emptyArticles.Visible = true;
		}
		else
		{
			foreach(var preview in previews)
				itemsList.Items.AddItem(new MainArticleUIPreview(preview, categories));
		}
	}
}